﻿using System;
using System.Collections.Generic;
using System.Text;
using UtilZ.Dotnet.Ex.Json;

namespace UtilZ.Dotnet.Ex.Model
{
    /// <summary>
    /// 序列化器基类
    /// </summary>
    public class DefaultJsonSerializer : SerializerAbs
    {
        private readonly JsonSerializeSetting _setting;

        /// <summary>
        /// 构造函数
        /// </summary>
        public DefaultJsonSerializer()
        {
            this._setting = JsonSerializeSetting.Default;
        }


        /// <summary>
        /// 将当前对象序列化为目标类型的对象
        /// </summary>
        /// <param name="obj">要序列化的对象</param>
        /// <returns>序列化后的数据</returns>
        protected override object PrimitiveSerialize(object obj)
        {
            return JsonSerializer.SerializeObject(obj, this._setting);
        }

        /// <summary>
        /// 将序列化数据反序列化到目标类型的对象
        /// </summary>
        /// <param name="targetObj">目标对象</param>
        /// <param name="serializeData">序列化数据</param>
        protected override void PrimitiveDeserialize(ref object targetObj, object serializeData)
        {
            string json = (string)serializeData;
            JsonSerializer.PopulateObject(json, ref targetObj, this._setting);
        }

    }
}
